$def with (list, editions, works, authors)

$var content_type = "application/x-bibtex"

$for e in editions:
    @Book{$e.key.split("/")[-1],
        title: {$:texsafe(e.title)},
        $if e.works:
            $ authors = e.works[0].get_authors()
        $else:
            $ authors = e.get_authors()
        $if authors:
            author: {$" and ".join(texsafe(a.name or "unnamed") for a in authors)},
        $if e.publishers:
            publisher: {$:texsafe(e.publishers[0])},
        $ year = e.get_publish_year()
        $if year:
            year: {$:texsafe(str(year))},
        $if e.publish_places:
            place: {$:texsafe(e.publish_places[0])},
    }

$for e in works:
    @Work{$e.key.split("/")[-1],
        title: {$:texsafe(e.title)},
        $if e.works:
            $ authors = e.works[0].get_authors()
        $else:
            $ authors = e.get_authors()
        $if authors:
            author: {$" and ".join(texsafe(a.name or "unnamed") for a in authors)},
        $if e.publishers:
            publisher: {$:texsafe(e.publishers[0])},
        $ year = e.first_publish_year
        $if year:
            first_publish_year: {$:texsafe(str(year))}
    }

$for e in authors:
    @Author{$e.key.split("/")[-1],
        name: {$:texsafe(e.name)},
        $ birth_date = e.birth_date
        $if birth_date:
            birth_date: {$:texsafe(str(birth_date))}
        $ death_date = e.death_date
        $if death_date:
            , death_date: {$:texsafe(str(death_date))}
    }